﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xyz.BusinessObjects;
using Xyz.DataObjects.Interfaces;

namespace Xyz.DataObjects.Entity.Dao
{
    public class EntityCustomerDao : ICustomerDao
    {
        public string GetCustomerTypeDescription(int customerType)
        {
            return customerType == 1 ? "Agent" : "Member";
        }

        public List<Customer> GetCustomersLike(string firstName, string lastName)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var customers = new List<Customer>();
                var query = context.CustomerEntities.Where(x => x.FirstName.Contains(firstName) && x.LastName.Contains(lastName)).ToList();

                query.ForEach(x => customers.Add(Mapper.Map(x)));

                return customers;
            }
        }

        public Customer LoginCustomer(string userName, string password)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var customer =
                    context.CustomerEntities.SingleOrDefault(x => x.UserName == userName && x.Password == password);

                if (customer != null)
                {
                    return Mapper.Map(customer);
                }
                return null;
            }
        }

        public void CreateCustomer(Customer customer)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                if (customer.GetType() == typeof(Member))
                {
                    MemberEntity entity = (MemberEntity)Mapper.Map(customer);
                    context.CustomerEntities.AddObject(entity);
                }
                else if (customer.GetType() == typeof(Agent))
                {
                    AgentEntity entity = (AgentEntity)Mapper.Map(customer);
                    context.CustomerEntities.AddObject(entity);
                }
                context.SaveChanges();
            }
        }

        public void UpdateCustomer(Customer customer)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                if (customer.GetType() == typeof(Member))
                {
                    MemberEntity entity = (MemberEntity)Mapper.Map(customer);
                    MemberEntity member = context.CustomerEntities.OfType<MemberEntity>().Single(x => x.CustomerId == customer.CustomerId);
                    member.Address1 = entity.Address1;
                    member.Address2 = entity.Address2;
                    member.Address3 = entity.Address3;
                    member.City = entity.City;
                    member.Country = entity.Country;
                    member.DateOfBirth = entity.DateOfBirth;
                    member.FirstName = entity.FirstName;
                    member.Gender = entity.Gender;
                    member.LastName = entity.LastName;
                    member.MemberPoints = entity.MemberPoints;
                    member.PassportExpiryDate = entity.PassportExpiryDate;
                    member.PassportNo = entity.PassportNo;
                    member.Password = entity.Password;
                    member.PostalCode = entity.PostalCode;
                    context.CustomerEntities.ApplyCurrentValues(member);
                }
                else if (customer.GetType() == typeof(Agent))
                {
                    AgentEntity entity = (AgentEntity)Mapper.Map(customer);
                    AgentEntity agent = context.CustomerEntities.OfType<AgentEntity>().Single(x => x.CustomerId == customer.CustomerId);
                    agent.CompanyName = entity.CompanyName;
                    agent.CustomerId = entity.CustomerId;
                    agent.DateOfBirth = entity.DateOfBirth;
                    agent.DiscountRate = entity.DiscountRate;
                    agent.FirstName = entity.FirstName;
                    agent.LastName = entity.LastName;
                    agent.Gender = entity.Gender;
                    agent.Password = entity.Password;
                    context.CustomerEntities.ApplyCurrentValues(agent);
                }

                context.SaveChanges();
            }
        }

        public bool IsUserNameExists(string userName)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var customer =
                    context.CustomerEntities.SingleOrDefault(x => x.UserName == userName);

                if (customer != null)
                {
                    return true;
                }
                return false;
            }
        }

        public void SubtractMemberPoints(int customerId, int pointsToSubtract)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var customer = context.CustomerEntities.OfType<MemberEntity>().SingleOrDefault(x => x.CustomerId == customerId);

                if (customer != null)
                {
                    customer.MemberPoints -= pointsToSubtract;
                }

                context.SaveChanges();
            }
        }
    }
}
